package com.ruoyi.business.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.KeyValueList;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.BusSpjgPeople;
import com.ruoyi.business.service.IBusSpjgPeopleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 就业人员Controller
 *
 * @author ruoyi
 * @date 2023-08-01
 */
@RestController
@RequestMapping("/spjg/people")
@Api(tags = "食品加工-就业人数")
public class BusSpjgPeopleController extends BaseController {
    @Autowired
    private IBusSpjgPeopleService busSpjgPeopleService;


    @GetMapping("/listData")
    @ApiOperation("图标数据")
    public AjaxResult listData(BusSpjgPeople busSpjgPeople) {
        startPage();
        List<BusSpjgPeople> list = busSpjgPeopleService.selectBusSpjgPeopleList(busSpjgPeople);
        List<String> keyList = new ArrayList<>();
        List<Object> valueList = new ArrayList<>();
        list.forEach(p->{
            Date time = p.getTime();
            String t = DateUtils.parseDateToStr(DateUtils.YYYYMM_NIAN, time);
            keyList.add(t);
            valueList.add(p.getCount());
        });
        Collections.reverse(keyList);
        Collections.reverse(valueList);
        return AjaxResult.success(new KeyValueList(keyList,valueList));
    }


    /**
     * 查询就业人员列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BusSpjgPeople busSpjgPeople) {
        startPage();
        List<BusSpjgPeople> list = busSpjgPeopleService.selectBusSpjgPeopleList(busSpjgPeople);
        return getDataTable(list);
    }

    /**
     * 导出就业人员列表
     */
    @PreAuthorize("@ss.hasPermi('spjg:people:export')")
    @Log(title = "就业人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusSpjgPeople busSpjgPeople) {
        List<BusSpjgPeople> list = busSpjgPeopleService.selectBusSpjgPeopleList(busSpjgPeople);
        ExcelUtil<BusSpjgPeople> util = new ExcelUtil<BusSpjgPeople>(BusSpjgPeople.class);
        util.exportExcel(response, list, "就业人员数据");
    }

    /**
     * 获取就业人员详细信息
     */
    @PreAuthorize("@ss.hasPermi('spjg:people:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(busSpjgPeopleService.selectBusSpjgPeopleById(id));
    }

    /**
     * 新增就业人员
     */
    @PreAuthorize("@ss.hasPermi('spjg:people:add')")
    @Log(title = "就业人员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusSpjgPeople busSpjgPeople) {
        return toAjax(busSpjgPeopleService.insertBusSpjgPeople(busSpjgPeople));
    }

    /**
     * 修改就业人员
     */
    @PreAuthorize("@ss.hasPermi('spjg:people:edit')")
    @Log(title = "就业人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusSpjgPeople busSpjgPeople) {
        return toAjax(busSpjgPeopleService.updateBusSpjgPeople(busSpjgPeople));
    }

    /**
     * 删除就业人员
     */
    @PreAuthorize("@ss.hasPermi('spjg:people:remove')")
    @Log(title = "就业人员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(busSpjgPeopleService.deleteBusSpjgPeopleByIds(ids));
    }
}
